const nodeExcel = require('@michaelray/style-xlsx'); // 导入插件
var fs = require("fs");
let path = require('path');
let PUBLIC_PATH = path.resolve(__dirname, '../../public/temp');
const initExcel=async (params)=>{
  let {fileName,Data,title}=params
  let list=[{name:fileName,data:Data}]
  const buffer = nodeExcel.build(list)
  let UUID=Math.random().toString(36).substring(2)+Date.now();
  const isExists=await fs.existsSync(PUBLIC_PATH)
  if(!isExists){
    fs.mkdirSync(PUBLIC_PATH,{recursive:true},(err)=>{
      if(err) return console.error("创建文件失败",err)
      console.log("目录创建成功");
    })
  }
  fs.writeFileSync(`${PUBLIC_PATH}/${UUID}.xlsx`,buffer,(err)=>{
    if (err) {
      console.log(err, "保存excel出错");
    } else {
      console.log("写入excel成功!!!");
    }
  })
  return `${UUID}.xlsx`
}
module.exports={
  initExcel
}